﻿using System;
using System.Collections.Generic;
#if DEBUG
using System.Diagnostics;
#endif // #if DEBUG
using System.Linq;
using System.Text;

using FRL.DebtChased.Extensions;
using FRL.DebtChased.Types;

namespace FRL.DebtChased.Repository.ViewModels
{

	/// <summary>
	/// Declaration of AccountInvoiceModel class
	/// </summary>
	[Serializable]
	public class AccountInvoiceModel
	{

		/////////////////////////////////////////////////////////////
		// Nested classes

		[Serializable]
		public class Element
		{

			/////////////////////////////////////////////////////////
			// Attributes

			public int AccountInvoiceId { get; set; }

			public string FRLRef { get; set; }

			public string ClientRef { get; set; }

			public string InvoiceNo { get; set; }

			public Money Amount { get; set; }
			public string AmountAsText
			{
				get { return Amount.ToString(); }
			}

			/////////////////////////////////////////////////////////
			// Construction

			public Element()
			{

			}

			public Element(DebtChasedEntities ent, int iAccountInvoiceId)
			{

				// Load record
				var result = (from ai in ent.AccountInvoices
							 join a in ent.Accounts on ai.AccountId equals a.AccountId
							 where (ai.AccountInvoiceId == iAccountInvoiceId)
							 select new { ai.AccountInvoiceId, a.FRLReference, a.ClientReference, ai.InvoiceNo, ai.AmountValue, ai.AmountCurrency }).FirstOrDefault();

				// Get the current code
				Money.eCurrency currency = Money.ParseCurrencyCode(result.AmountCurrency);

				// Initialize member variables
				this.AccountInvoiceId = result.AccountInvoiceId;
				this.Amount = new Money(result.AmountValue, currency);
				this.ClientRef = result.ClientReference;
				this.FRLRef = result.FRLReference;
				this.InvoiceNo = result.InvoiceNo;

			}

		}

		/////////////////////////////////////////////////////////
		// Attributes

		private List<Element> m_elements = new List<Element>();
		public List<Element> Elements
		{
			get { return m_elements; }
		}

		/////////////////////////////////////////////////////////
		// Construction

		public AccountInvoiceModel()
		{

			// Initialize member variables
			m_elements = new List<Element>();

		}

		public AccountInvoiceModel(DebtChasedEntities ent, string strSearch)
		{

			// Lookup the invoices
			var results = from ai in ent.AccountInvoices
						  join a in ent.Accounts on ai.AccountId equals a.AccountId
						  where (
							(a.FRLReference.Contains(strSearch))
							||
							(a.ClientReference.Contains(strSearch))
						  )
						  select new { ai.AccountInvoiceId, a.FRLReference, a.ClientReference, ai.InvoiceNo, ai.AmountValue, ai.AmountCurrency };
			foreach (var result in results)
			{

				// Get the current code
				Money.eCurrency currency = Money.ParseCurrencyCode(result.AmountCurrency);

				// Load the elements
				m_elements.Add( new Element() {
					AccountInvoiceId = result.AccountInvoiceId,
					Amount = new Money(result.AmountValue, currency),
					ClientRef = result.ClientReference,
					FRLRef = result.FRLReference, 
					InvoiceNo = result.InvoiceNo
				});

			}

		}

		/////////////////////////////////////////////////////////
		// Operations

		public AccountInvoiceModel.Element FindByAccountInvoiceId(int iAccountInvoiceId)
		{

			// Declare return variabe
			AccountInvoiceModel.Element Ftmp = null;

			// Loop through list
			foreach(AccountInvoiceModel.Element el in this.Elements)
			{

				// Compare ids
				if(el.AccountInvoiceId == iAccountInvoiceId)
				{

					// Update return variable and break out of loop
					Ftmp = el;
					break;

				}

			}

			// Return variabe
			return Ftmp;

		}

	}

}
